package com.qf.main.pharmacy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.main.pharmacy.common.vo.ClassificationCountVo;
import com.qf.main.pharmacy.common.vo.OrderAnalyseListVo;
import com.qf.main.pharmacy.common.vo.OrderAnalyseVo;
import com.qf.main.pharmacy.common.vo.OrderMallVo;
import com.qf.main.pharmacy.entity.Order;
import com.qf.main.pharmacy.entity.OrderMall;
import com.qf.main.pharmacy.entity.Pharmacy;
import com.qf.main.pharmacy.mapper.OrderMallMapper;
import com.qf.main.pharmacy.mapper.OrderMapper;
import com.qf.main.pharmacy.mapper.PharmacyClassificationMapper;
import com.qf.main.pharmacy.mapper.PharmacyMapper;
import com.qf.main.pharmacy.service.AnalyseService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author chengqiang
 * @Create 2024-02-25-20:14
 * @Description:
 */
@Service
public class AnalyseServiceImpl implements AnalyseService {

    @Resource
    PharmacyMapper pharmacyMapper;
    @Resource
    OrderMallMapper orderMallMapper;
    @Resource
    OrderMapper orderMapper;
    @Resource
    @Lazy
    AnalyseService analyseService;
    @Resource
    PharmacyClassificationMapper pharmacyClassificationMapper;

    @Override
    public List<OrderAnalyseVo> listOrderAnalyse() {
        QueryWrapper<Pharmacy> qw = new QueryWrapper<>();
        List<Pharmacy> pharmacies = pharmacyMapper.selectList(qw);
        List<OrderAnalyseVo> orderAnalyseVos = CommonBeanutils.convertListTo(pharmacies, OrderAnalyseVo::new);

        orderAnalyseVos.stream().forEach(orderAnalyseVo->{
            // 今天
            QueryWrapper<OrderMall> queryWrapper =new QueryWrapper<>();
            queryWrapper.apply("DATE(create_date) = CURDATE()");
            queryWrapper.eq(OrderMall.COL_ORDER_MALL_ID,orderAnalyseVo.getPharmacyId());

            Long todayBuy = orderMallMapper.selectCount(queryWrapper);
            queryWrapper.eq(OrderMall.COL_ORDER_MALL_STATUS,0);

            orderAnalyseVo.setTodayBuy(todayBuy.intValue());

            QueryWrapper<OrderMall> queryWrapper2 =new QueryWrapper<>();
            queryWrapper2.apply("YEAR(create_date) = YEAR(CURDATE()) AND MONTH(create_date) = MONTH(CURDATE())");
            queryWrapper2.eq(OrderMall.COL_ORDER_MALL_ID,orderAnalyseVo.getPharmacyId());
            Long monthBuy = orderMallMapper.selectCount(queryWrapper2);
            orderAnalyseVo.setMonthBuy(monthBuy.intValue());

            QueryWrapper<OrderMall> queryWrapper3 =new QueryWrapper<>();
            queryWrapper3.eq(OrderMall.COL_ORDER_MALL_ID,orderAnalyseVo.getPharmacyId());
            Long totalBuy = orderMallMapper.selectCount(queryWrapper3);
            orderAnalyseVo.setTotalBuy(totalBuy.intValue());
        });
        Collections.sort(orderAnalyseVos,new Comparator<OrderAnalyseVo>(){
            @Override
            public int compare(OrderAnalyseVo o1, OrderAnalyseVo o2) {
                if(o1.getTodayBuy() < o2.getTodayBuy()){
                    return 1;
                }
                if(o1.getTodayBuy() == o2.getTodayBuy()){
                    return 0;
                }
                return -1;
            }
        });
        return orderAnalyseVos;
    }
    @Override
    public OrderAnalyseListVo getOrderAnalyseList() {
        OrderAnalyseListVo orderAnalyseListVo  = new OrderAnalyseListVo();
        // 今天
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.apply("DATE(create_date) = CURDATE()");
        List<Order> todayOrders = orderMapper.selectList(qw);
        for (Order order : todayOrders) {
            QueryWrapper<OrderMall> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq(OrderMall.COL_ORDER_NO,order.getOrderNo());
            if(order.getOrderStatus()==0){
                orderAnalyseListVo.setCancelTodayBuy(orderAnalyseListVo.getCancelTodayBuy()+orderMallMapper.selectCount(queryWrapper).intValue());
            }else if(order.getOrderStatus()==1){
                orderAnalyseListVo.setNoPayTodayBuy(orderAnalyseListVo.getNoPayTodayBuy()+orderMallMapper.selectCount(queryWrapper).intValue());
            }else if(order.getOrderStatus()==2){
                orderAnalyseListVo.setPayTodayBuy(orderAnalyseListVo.getPayTodayBuy()+orderMallMapper.selectCount(queryWrapper).intValue());
            }
        }
        QueryWrapper<Order> qw2 = new QueryWrapper<>();

        qw2.apply("YEAR(create_date) = YEAR(CURDATE()) AND MONTH(create_date) = MONTH(CURDATE())");
        List<Order> MonthOrders = orderMapper.selectList(qw2);
        for (Order order : MonthOrders) {
            QueryWrapper<OrderMall> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq(OrderMall.COL_ORDER_NO,order.getOrderNo());
            if(order.getOrderStatus()==0){
                orderAnalyseListVo.setCancelMonthBuy(orderAnalyseListVo.getCancelMonthBuy()+orderMallMapper.selectCount(queryWrapper).intValue());
            }else if(order.getOrderStatus()==1){
                orderAnalyseListVo.setNoPayMonthBuy(orderAnalyseListVo.getNoPayMonthBuy()+orderMallMapper.selectCount(queryWrapper).intValue());
            }else if(order.getOrderStatus()==2){
                orderAnalyseListVo.setPayMonthBuy(orderAnalyseListVo.getPayMonthBuy()+orderMallMapper.selectCount(queryWrapper).intValue());
            }
        }


        List<OrderAnalyseVo> orderAnalyseVos = analyseService.listOrderAnalyse();
        orderAnalyseListVo.setOrderAnalyseList(orderAnalyseVos);
        return orderAnalyseListVo;
    }

    @Override
    public List<Map<String, Object>> countDataPerDayLast7Days() {

        List<Map<String, Object>> maps = orderMapper.selectDataCountByDay(null);
        return maps;
    }
    public List<Map<String, Object>> countCancelDataPerDayLast7Days() {
        List<Map<String, Object>> maps = orderMapper.selectDataCountByDay(0);
        return maps;
    }
    public List<Map<String, Object>> countNoPayCancelDataPerDayLast7Days() {
        List<Map<String, Object>> maps = orderMapper.selectDataCountByDay(1);
        return maps;
    }
    public List<Map<String, Object>> countPayCancelDataPerDayLast7Days() {
        List<Map<String, Object>> maps = orderMapper.selectDataCountByDay(2);
        return maps;
    }


    public List<ClassificationCountVo> classificationCount(){
        List<ClassificationCountVo> maps = pharmacyClassificationMapper.selectDataCountByClassification();
        return maps;
    }
    public List<ClassificationCountVo> classificationMallCount(){
        List<ClassificationCountVo> maps = pharmacyClassificationMapper.selectDataCountByOrderMallClassification();
        return maps;
    }
}
